<template>
  <div class="common-wrap">
    <QueryForm
      ref="conditionForm"
      module="importFields"
      :searchBtnLoading="loading"
      v-model:searchForm="searchForm"
      v-model:search-column="searchColumn"
      @handle-search="onSearch"
    />
    <div class="common-box">
      <el-row style="height: 40px" class="con-button-rows">
        <ButtonGroup
          :disabledDelete="multipleSelection.length != 1"
          :disabledEdit="multipleSelection.length != 1"
          :hasPermiAdd="['common:importFields:add']"
          :hasPermiDelete="['common:importFields:delete']"
          :buttonType="['add', 'delete']"
          @add="onAdd"
          @delete="onDelete"
        ></ButtonGroup>
      </el-row>
      <MyTable
        tableId="importFields"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pageVo="pageVo"
        ref="commonTable"
        moduleName="importFields"
        @handleSelectionChange="handleSelectionChange"
        @handle-page-change="handlePageChange"
      ></MyTable>
    </div>
    <Add
      v-if="dialogVisible"
      v-model:visible="dialogVisible"
      :currRow="currRow"
      :isType="isType"
      @onSubmit="onSearch"
    ></Add>
  </div>
</template>

<script setup lang='tsx'>
import { ref, reactive, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import Add from "./components/add.vue";
import * as api from "@/api/index.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { hasPermi } from "@/utils/mUtils.js";
let { t } = useI18n();

defineOptions({
  name: "ImportFields",
});
const pageVo = reactive({
  pageSize: 50,
  total: 10,
  currentPage: 1,
  status: true,
});
const loading = ref(true);
const dataSource = ref([]);
const columns = ref([
  {
    type: "checkbox",
    width: 50,
    align: "center",
    fixed: "left",
  },
  {
    title: "序号",
    type: "seq",
    width: 60,
    fixed: "left",
  },
  {
    field: "moduleName",
    title: "模块名称",
    slots: {
      default: ({ row }) => {
        return [
          <el-button
            type="primary"
            disabled={!hasPermi(["common:importFields:edit"])}
            link
            onClick={() => {
              onEdit(row);
            }}
          >
            {row.moduleName || ""}
          </el-button>
        ];
      },
    },
  },
  {
    field: "createTime",
    title: "创建时间",
  },
  {
    field: "createByName",
    title: "创建人",
  },
  {
    field: "updateTime",
    title: "修改时间",
  },
  {
    field: "updateByName",
    title: "修改人",
  },
]);
const searchForm = ref({});
const searchColumn = ref([
  {
    label: "模块名称",
    type: "label_input",
    prop: "name",
  },
]);
const dialogVisible = ref(false);
const multipleSelection = ref([]);
const currRow = ref({});
const isType = ref("add");
onMounted(() => {
  onSearch();
});
const list = () => {
  loading.value = true;
  api.default.common.importFields
    .list({
      ...searchForm.value,
      pageNum: pageVo.currentPage,
      pageSize: pageVo.pageSize,
    })
    .then((res) => {
      dataSource.value = res.rows;
      loading.value = false;
      pageVo.total = res.total;
    })
    .catch(() => {
      loading.value = false;
    });
};

const onAdd = () => {
  isType.value = "add";
  if (multipleSelection.value.length != 0) {
    currRow.value = multipleSelection.value[0];
  }
  dialogVisible.value = true;
};
const onEdit = (row) => {
  isType.value = "edit";
  currRow.value = { ...row };
  dialogVisible.value = true;
};
const onDelete = () => {
  const ids = multipleSelection.value.map((item) => item.id);
  ElMessageBox.confirm(t("是否确定删除当前选择数据？"), t("提示"), {
    confirmButtonText: t("确定"),
    cancelButtonText: t("取消"),
    type: "warning",
  })
    .then(() => {
      api.default.common.importFields.remove(ids).then(() => {
        ElMessage.success(t("删除成功"));
        onSearch();
      });
    })
    .catch(() => {});
};
const handleSelectionChange = (list) => {
  multipleSelection.value = list;
};
const handlePageChange = (val) => {
  const { currentPage, pageSize } = val;
  pageVo.currentPage = currentPage;
  pageVo.pageSize = pageSize;
  list();
};
const onSearch = () => {
  pageVo.currentPage = 1;
  list();
};
</script>

<style lang="scss" scoped>
.pcMenu {
  height: 100%;
  width: 100%;
  background: #fff;
}
</style>